package com.trainfinder.ripper;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.jdom.Attribute;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.Namespace;
import org.jdom.xpath.XPath;

import com.trainfinder.datapersistace.TicketType;
import com.trainfinder.requestor.UnparsableException;

abstract class AbstractParser {

	protected static final String XPATH_STR = "/xhtml:html/xhtml:body/xhtml:div/"
			+ "xhtml:div[@id='center_01']/xhtml:form/"
			+ "xhtml:div[@id='calendar']/xhtml:table/" + "xhtml:tbody/xhtml:tr";

	protected static final Namespace NS = Namespace.getNamespace("xhtml",
			"http://www.w3.org/1999/xhtml");

	protected Ticket[] getTickets(Document jdomDocument, int columnCount)
			throws UnparsableException {
		Collection<Ticket> result = new ArrayList<Ticket>();

		String lableXPath = "/xhtml:html/xhtml:body/xhtml:div/"
				+ "xhtml:div[@id='center_01']/xhtml:form/"
				+ "xhtml:div[@id='calendar']/xhtml:table/"
				+ "xhtml:thead/xhtml:tr[last()]";
		 
		for (int u = 4; u <= columnCount; u++) {

			final String xPath = lableXPath + "/xhtml:th[" + u + "]/xhtml:a";

			try {

				final XPath xp = XPath.newInstance(xPath);
				xp.addNamespace(NS);
				final Element e = (Element) xp.selectSingleNode(jdomDocument);
				if (e == null) {
					result.add(new TicketImpl(TicketType.Unknown,
							TicketClass.Unknown));
				} else {
					String ticketType = e.getText().trim();
					Attribute attrib = e.getAttribute("href");
					String href = attrib.getValue();

					final TicketType type;
					if ("Semi flexible".equals(ticketType))
						type = TicketType.SEMI_FLEXIBLE;
					else if ("Non flexible".equals(ticketType))
						type = TicketType.NON_FLEXIBLE;
					else if ("Fully flexible".equals(ticketType))
						type = TicketType.FULLY_FLEXIBLE;
					else
						throw new UnparsableException("unknown ticket type");

					final TicketClass ticketClass;
					if (href.endsWith("standard_fare_conditions.jsp"))
						ticketClass = TicketClass.Standard;
					else if (href
							.endsWith("leisure_select_fare_conditions.jsp"))
						ticketClass = TicketClass.Leisure;
					else if (href.endsWith("business_fare_conditions.jsp"))
						ticketClass = TicketClass.Business;
					else
						throw new UnparsableException("unknown ticket class");

					result.add(new TicketImpl(type, ticketClass));

				}
			} catch (JDOMException e) {
				throw new UnparsableException(e);
			}
		}
		return (Ticket[]) result.toArray(new Ticket[result.size()]);
	}

	protected int getColumnCount(Document jdomDocument) {
		String xPath = XPATH_STR + "[1]/xhtml:td";
		return countNodeList(xPath, jdomDocument);

	}

	protected int countNodeList(String xPath, Document jdomDocument) {
		List list = new ArrayList();
		{
			XPath xp;
			try {
				xp = XPath.newInstance(xPath);
				xp.addNamespace(NS);
				list = (xp.selectNodes(jdomDocument));
			} catch (JDOMException e) {
			//	e.printStackTrace();
			}

		}
		return list.size();
	}

	protected int getRowCount(Document jdomDocument) {
		return countNodeList(XPATH_STR, jdomDocument);
	}

}
